package com.example.busshop.entity;

import com.example.busshop.common.entity.LogicEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.util.Date;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
@Setter
@Getter
@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler","fieldHandler"})

@Entity
@Where(clause = "is_deleted=0")
@DiscriminatorValue("2")
public class Business extends User {
    @Column
    private String name;
    @OneToMany(mappedBy = "business",
               fetch = FetchType.LAZY,
               orphanRemoval = true,
               cascade = CascadeType.ALL)
    @JsonIgnore
    private Set<Shop> shops;
    /**
     * 创建门店
     * @return
     */
    public Shop createShop (String name){
        AtomicReference<Shop> result=new AtomicReference<>(null);
        shops.forEach(shop->{
            if(shop.getName().equals(name)){
                result.set(shop);
            }
        });
        Shop shop=new Shop();
        shop.setName(name);
        shop.setCreateTime(new Date());

        shops.add(shop);
        result.set(shop);
        return result.get();
    }
}
